import {BigNumber, bigNumberToFixed} from "@/util/digit";


export const compareAmount = (value, decimalLength = 2, type = 'input') => {
  if (value === '') {
    return ''
  }
  let {value: newValue, baseLimitLength} = filter(value)
  // 如果为空字符串，那么设置为0，同时将值转为Number类型
  newValue = newValue === '' ? 0 : +value
  // 对比最大最小值，取在min和max之间的值
  console.log('newValue', newValue)
  let minLength = Math.min(baseLimitLength, decimalLength)
  if (minLength < 0) {
    minLength = 0
  }
  if (decimalLength > 0) {
    newValue = bigNumberToFixed(newValue, minLength, false, 1)
    console.log('newValue', newValue)
  }
  if (baseLimitLength === 0) {
    return newValue + '.'
  }
  return newValue
}

export const filter = (value, decimalLength) => {
  // 只允许0-9之间的数字，"."为小数点，"-"为负数时候使用
  value = String(value).replace(/[^0-9.-]/g, '')
  const baseLimitLength = getLimitLength(value)
  // 如果只允许输入整数，则过滤掉小数点后的部分
  if (decimalLength <= 0 && value.indexOf('.') !== -1) {
    value = value.split('.')[0]
  }
  return {value, baseLimitLength};
}
export const getLimitLength = (value) => {
  try {
    return value.split('.')[1].length
  }catch (err) {
    return -1
  }
}
